//
//  MarqueeView.swift
//  EasyDana
//
//  Created by mac on 2024/10/10.
//

import UIKit
import RxSwift
import RxCocoa
class MarqueeView: UIView {
    
    private let bag = DisposeBag()
    
    let source = PublishSubject<[String]>()
    
    private var src: [String] = []
    
//    @IBOutlet weak var marqueeView: UUMarqueeView!
    
    @IBOutlet weak var marqueeView: UUMarqueeView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.marqueeView.timeIntervalPerScroll = 2.0
        self.marqueeView.timeDurationPerScroll = 1
        self.marqueeView.delegate = self
        self.marqueeView.direction = .upward
        self.source.subscribe(onNext: { [weak self] elements in
            self?.src = elements
            self?.marqueeView.reloadData()
        })
        .disposed(by: self.bag)
    }
    
}

extension MarqueeView: UUMarqueeViewDelegate {
    func updateItemView(_ itemView: UIView!, at index: UInt, for marqueeView: UUMarqueeView!) {
        let label = itemView.viewWithTag(1001) as! UILabel
        label.text = src[Int(index)]
    }
    
    func numberOfVisibleItems(for marqueeView: UUMarqueeView!) -> UInt {
        return 1
    }
    
    func numberOfData(for marqueeView: UUMarqueeView!) -> UInt {
        return UInt(self.src.count)
    }
    
    func createItemView(_ itemView: UIView!, for marqueeView: UUMarqueeView!) {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 12)
        label.tag = 1001
        label.frame = itemView.bounds
        itemView.addSubview(label)
    }
    
    func itemViewWidth(at index: UInt, for marqueeView: UUMarqueeView!) -> CGFloat {
        return 100
    }
}
